home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 168 / hd.c < prev    next >
C/C++ Source or Header  |  1988-03-31  |  1KB  |  67 lines

  1. /* dump the contents of a file in hex words */
  2.  
  3. #include <stdio.h>
  4.  
  5. main(argc, argv) int argc; char *argv[]; {
  6.     int i;
  7.     FILE *f;
  8.     for (i = 1; i < argc; i++) {
  9.         printf("%s\n", argv[i]);
  10.         if ((f = fopen(argv[i], "rb")) != NULL) {
  11.             hd(f);
  12.             fclose(f);
  13.         }
  14.     }
  15.     return 0;
  16. }
  17.  
  18. hd(f) FILE *f; {
  19.     int offset, cnt, hi, lo, i, word[8];
  20.     offset = 0;
  21.     cnt = 8;
  22.     while (cnt == 8) {
  23.         puthex(offset, 4);
  24.         putchar(':');
  25.         putchar(' ');
  26.         for (cnt = 0; cnt < 8; ) {
  27.             if ((hi = getc(f)) == EOF) break;
  28.             offset++;
  29.             if ((lo = getc(f)) == EOF) {
  30.                 word[cnt++] = (hi << 8);
  31.                 break;
  32.             }
  33.             else    {
  34.                 word[cnt++] = (hi << 8) + lo;
  35.                 offset++;
  36.             }
  37.         }
  38.         for (i = 0; i < cnt; i++) {
  39.             puthex(word[i], 4);
  40.             putchar(' ');
  41.         }
  42.         for ( ; i < 8; i++) printf("     ");
  43.         printf(" | ");
  44.         for (i = 0; i < cnt; i++) {
  45.             putcx(word[i] >> 8);
  46.             putcx(word[i]);
  47.         }
  48.         putchar('\n');
  49.     }
  50.     if (cnt != 0) {
  51.         puthex(offset, 4);
  52.         putchar('\n');
  53.     }
  54. }
  55.  
  56. puthex(n, size) {
  57.     if (size > 1) puthex(n >> 4, size - 1);
  58.     putchar("0123456789ABCDEF"[n & 15]);
  59. }
  60.  
  61. putcx(c) {
  62.     c = c & 255;
  63.     if (c >= 32 && c <= 127) putchar(c);
  64.     else putchar('.');
  65. }
  66.  
  67.